package org.mock.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.mock.common.ResultBaiFenHui;
import org.mock.dto.BaifenhuiNoticeDTO;
import org.mock.dto.BaifenhuiOrderProductDTO;
import org.mock.entity.BaifenhuiNotice;
import org.mock.entity.BaifenhuiOrderBase;
import org.mock.entity.BaifenhuiOrderProduct;
import org.mock.enums.BaiFenHuiNoticeType;
import org.mock.enums.BaiFenHuiOrderStatus;
import org.mock.enums.BaiFenHuiProductState;
import org.mock.mapper.BaifenhuiOrderBaseMapper;
import org.mock.mapper.BaifenhuiOrderProductMapper;
import org.mock.request.baiFenHui.OrderInfoByCustomerRequest;
import org.mock.request.baiFenHui.OrderInfoRequest;
import org.mock.response.baiFenHui.BaiFenHuiOrderVo;
import org.mock.service.BaifenhuiNoticeService;
import org.mock.service.BaifenhuiOrderBaseService;
import org.mock.dto.BaifenhuiOrderBaseDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mock.common.Condition;
import org.mock.service.BaifenhuiOrderProductService;
import org.mock.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import cn.hutool.core.util.StrUtil;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 订单基本信息 服务实现类
 *
 * @author AI
 * @since 2025-05-23
 */
@Service
@AllArgsConstructor
@Slf4j
public class BaifenhuiOrderBaseServiceImpl implements BaifenhuiOrderBaseService {

    protected BaifenhuiOrderBaseMapper baifenhuiOrderBaseMapper;
    protected BaifenhuiOrderProductMapper baifenhuiOrderProductMapper;
    protected BaifenhuiOrderProductService baifenhuiOrderProductService;
    protected BaifenhuiNoticeService baifenhuiNoticeService;

    @Override
    public IPage<BaifenhuiOrderBase> page(BaifenhuiOrderBaseDTO dto) {
        IPage<BaifenhuiOrderBase> page = Condition.getPage(dto);
        QueryWrapper<BaifenhuiOrderBase> queryWrapper = Condition.getQueryWrapper(BeanCopyUtils.copy(dto, BaifenhuiOrderBase.class));
        if (StrUtil.isNotEmpty(dto.getColumn()) && StrUtil.isNotEmpty(dto.getKeywords())) {
            queryWrapper.like(dto.getColumn(), dto.getKeywords());
        }
        if (StrUtil.isNotEmpty(dto.getOrderAsc())) {
            queryWrapper.orderByAsc(dto.getOrderAsc());
        }
        if (StrUtil.isNotEmpty(dto.getOrderDesc())) {
            queryWrapper.orderByDesc(dto.getOrderDesc());
        }
        if (dto.getStartTime() != null) {
            queryWrapper.gt("created", dto.getStartTime());
        }
        if (dto.getEndTime() != null) {
            queryWrapper.lt("created", dto.getEndTime());
        }
        return baifenhuiOrderBaseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<BaifenhuiOrderBase> list(BaifenhuiOrderBaseDTO dto) {
        QueryWrapper<BaifenhuiOrderBase> queryWrapper = Condition.getQueryWrapper(BeanCopyUtils.copy(dto, BaifenhuiOrderBase.class));
        if (StrUtil.isNotEmpty(dto.getColumn()) && StrUtil.isNotEmpty(dto.getKeywords())) {
            queryWrapper.like(dto.getColumn(), dto.getKeywords());
        }
        if (StrUtil.isNotEmpty(dto.getOrderAsc())) {
            queryWrapper.orderByAsc(dto.getOrderAsc());
        }
        if (StrUtil.isNotEmpty(dto.getOrderDesc())) {
            queryWrapper.orderByDesc(dto.getOrderDesc());
        }
        if (dto.getStartTime() != null) {
            queryWrapper.gt("created", dto.getStartTime());
        }
        if (dto.getEndTime() != null) {
            queryWrapper.lt("created", dto.getEndTime());
        }
        return baifenhuiOrderBaseMapper.selectList(queryWrapper);
    }

    @Override
    public Integer save(BaifenhuiOrderBaseDTO dto) {
        return baifenhuiOrderBaseMapper.insert(BeanCopyUtils.copy(dto, BaifenhuiOrderBase.class));
    }

    @Override
    public Integer updateById(BaifenhuiOrderBaseDTO dto) {
        return baifenhuiOrderBaseMapper.updateById(BeanCopyUtils.copy(dto, BaifenhuiOrderBase.class));
    }


    @Override
    public Integer deleteLogic(List<Integer> toIntList) {
        return baifenhuiOrderBaseMapper.deleteBatchIds(toIntList);
    }

    @Override
    public BaifenhuiOrderBase getOne(BaifenhuiOrderBaseDTO dto) {
        return baifenhuiOrderBaseMapper.selectOne(Condition.getQueryWrapper(BeanCopyUtils.copy(dto, BaifenhuiOrderBase.class)));
    }

    @Override
    public ResultBaiFenHui getOrderInfo(OrderInfoRequest request) {
        try {
            String orderSn = request.getOrder_sn();
            log.info("查询订单基本信息");
            BaifenhuiOrderBase orderBase = baifenhuiOrderBaseMapper.selectOne(new LambdaQueryWrapper<BaifenhuiOrderBase>()
                    .eq(BaifenhuiOrderBase::getOrderSn, orderSn)
            );
            log.info("查询订单商品信息");
            List<BaifenhuiOrderProduct> orderProductList = baifenhuiOrderProductMapper.selectList(new LambdaQueryWrapper<BaifenhuiOrderProduct>()
                    .eq(BaifenhuiOrderProduct::getOrderId, orderBase.getOrderId())
            );

            BaiFenHuiOrderVo orderVo = new BaiFenHuiOrderVo();
            orderVo.setBase(orderBase);
            orderVo.setProduct(orderProductList);
            return ResultBaiFenHui.success(orderVo);
        } catch (Exception e) {
            log.error("异常：{}", e.getMessage(), e);
            return ResultBaiFenHui.error("999", e.getMessage());
        }
    }

    @Override
    public ResultBaiFenHui getOrderInfoByCustomer(OrderInfoByCustomerRequest request) {
        try {
            String customerNo = request.getCustomer_no();

            //查询订单基本信息
            BaifenhuiOrderBase orderBase = baifenhuiOrderBaseMapper.selectOne(new LambdaQueryWrapper<BaifenhuiOrderBase>()
                    .eq(BaifenhuiOrderBase::getOrderId, customerNo)
            );
            //查询订单商品信息
            List<BaifenhuiOrderProduct> orderProductList = baifenhuiOrderProductMapper.selectList(new LambdaQueryWrapper<BaifenhuiOrderProduct>()
                    .eq(BaifenhuiOrderProduct::getOrderId, customerNo)
            );

            BaiFenHuiOrderVo orderVo = new BaiFenHuiOrderVo();
            orderVo.setBase(orderBase);
            orderVo.setProduct(orderProductList);
            return ResultBaiFenHui.success(orderVo);
        } catch (Exception e) {
            log.error("异常：{}", e.getMessage(), e);
            return ResultBaiFenHui.error("999", e.getMessage());
        }
    }

    @Override
    public ResultBaiFenHui orderFinish(String orderId) {
        log.info("开始将订单出库");
        try {
            //更新订单基本信息的状态
            BaifenhuiOrderBaseDTO query = new BaifenhuiOrderBaseDTO();
            query.setOrderId(orderId);
            BaifenhuiOrderBase orderBase = this.getOne(query);
            orderBase.setStatus(BaiFenHuiOrderStatus.FINISH.getValue());
            baifenhuiOrderBaseMapper.updateById(orderBase);

            //更新订单商品的状态
            BaifenhuiOrderProductDTO productQuery = new BaifenhuiOrderProductDTO();
            productQuery.setOrderId(orderId);
            BaifenhuiOrderProduct orderProduct = baifenhuiOrderProductService.getOne(productQuery);
            orderProduct.setState(BaiFenHuiProductState.FINISH.getValue());
            baifenhuiOrderProductMapper.updateById(orderProduct);

            //增加通知消息
            BaifenhuiNoticeDTO notice = new BaifenhuiNoticeDTO();
            String current = DateUtil.format(new Date(), "MMddHHmmss");
            notice.setNoticeId(Integer.parseInt(current));
            notice.setTime(LocalDateTime.now());
            notice.setType(BaiFenHuiNoticeType.ELEVEN.getValue());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("order_sn", orderBase.getOrderSn());
            jsonObject.put("itemId", orderProduct.getItemId());
            notice.setResult(jsonObject.toJSONString());
            baifenhuiNoticeService.save(notice);
            return ResultBaiFenHui.success("订单出库完成");
        } catch (Exception e) {
            log.error("异常：{}", e.getMessage(), e);
            return ResultBaiFenHui.error("999", e.getMessage());
        }
    }
}
